package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsYDetailBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsYDetailVo;
import com.ruoyi.zhgd.domain.NLaboratoryMechanicsYDetail;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsYDetailMapper;
import com.ruoyi.zhgd.service.INLaboratoryMechanicsYDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 压力机详情信息Service业务层处理
 *
 * @author tll
 * @date 2023-09-15
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryMechanicsYDetailServiceImpl implements INLaboratoryMechanicsYDetailService {

    private final NLaboratoryMechanicsYDetailMapper baseMapper;

    /**
     * 查询压力机详情信息
     */
    @Override
    public NLaboratoryMechanicsYDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询压力机详情信息列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsYDetailVo> queryPageList(NLaboratoryMechanicsYDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMechanicsYDetail> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsYDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询压力机详情信息列表
     */
    @Override
    public List<NLaboratoryMechanicsYDetailVo> queryList(NLaboratoryMechanicsYDetailBo bo) {
        LambdaQueryWrapper<NLaboratoryMechanicsYDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMechanicsYDetail> buildQueryWrapper(NLaboratoryMechanicsYDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMechanicsYDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGroupId() != null, NLaboratoryMechanicsYDetail::getGroupId, bo.getGroupId());
        lqw.eq(bo.getXH() != null, NLaboratoryMechanicsYDetail::getXH, bo.getXH());
        lqw.eq(bo.getShape() != null, NLaboratoryMechanicsYDetail::getShape, bo.getShape());
        lqw.eq(StringUtils.isNotBlank(bo.getSize()), NLaboratoryMechanicsYDetail::getSize, bo.getSize());
        lqw.eq(StringUtils.isNotBlank(bo.getQDDJ()), NLaboratoryMechanicsYDetail::getQDDJ, bo.getQDDJ());
        lqw.eq(StringUtils.isNotBlank(bo.getPingZhong()), NLaboratoryMechanicsYDetail::getPingZhong, bo.getPingZhong());
        lqw.eq(bo.getXS() != null, NLaboratoryMechanicsYDetail::getXS, bo.getXS());
        lqw.eq(bo.getLoad() != null, NLaboratoryMechanicsYDetail::getLoad, bo.getLoad());
        lqw.eq(bo.getStress() != null, NLaboratoryMechanicsYDetail::getStress, bo.getStress());
        lqw.eq(bo.getTestBegin() != null, NLaboratoryMechanicsYDetail::getTestBegin, bo.getTestBegin());
        lqw.eq(bo.getTestEnd() != null, NLaboratoryMechanicsYDetail::getTestEnd, bo.getTestEnd());
        lqw.eq(StringUtils.isNotBlank(bo.getYSGCSJ()), NLaboratoryMechanicsYDetail::getYSGCSJ, bo.getYSGCSJ());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NLaboratoryMechanicsYDetail::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增压力机详情信息
     */
    @Override
    public Boolean insertByBo(NLaboratoryMechanicsYDetailBo bo) {
        NLaboratoryMechanicsYDetail add = BeanUtil.toBean(bo, NLaboratoryMechanicsYDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改压力机详情信息
     */
    @Override
    public Boolean updateByBo(NLaboratoryMechanicsYDetailBo bo) {
        NLaboratoryMechanicsYDetail update = BeanUtil.toBean(bo, NLaboratoryMechanicsYDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMechanicsYDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除压力机详情信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 压力机设备详情信息列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsYDetailVo> page(NLaboratoryMechanicsYDetailBo bo, PageQuery pageQuery) {
        if (ObjectUtils.isEmpty(bo.getId())) {
            throw new ServiceException("主表id不能为空!");
        }
        Page<NLaboratoryMechanicsYDetailVo> result = baseMapper.page(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    @Override
    public List<NLaboratoryMechanicsYDetail> selectyDetail(Integer id) {
        return baseMapper.selectyDetail(id);
    }
}
